<template>
  <div class="app-container">
    <div class="common-card damConfigForm">
      <div class="title mb-5">{{ isEdit ? "编辑" : "新增" }}用户</div>
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="180px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="用户名" prop="accountNo">
              <el-input v-model="form.accountNo" :readonly="isEdit"  :disabled="isEdit"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="昵称" prop="userName">
              <el-input v-model="form.userName" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type = "password" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话" prop="telPhone">
              <el-input v-model="form.telPhone" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="nickName">
              <el-input v-model="form.nickName" @input="handleInput" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="年龄" prop="sex">
              <el-slider
                v-model="form.age"
                :min="18"
                :max="80"
                show-input
              ></el-slider>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="养殖面积" prop="telPhone">
              <el-input v-model="form.area" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家庭地址" prop="nickName">
              <el-input v-model="form.address" @input="handleInput" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
        
        </el-row>
      </el-form>
      <div class="actionBox">
        <el-button type="success" @click="handleSubmit">保存</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserDetail, addUser, updateUser } from '@/api/user'

export default {
  data() {
    return {
      form: { },
      rules: {
        name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        supervisorPhone: [
          { required: true, message: '请输入负责手机号码', trigger: 'blur' },
          {
            pattern: /$/,
            message: '请检查手机号码格式',
            trigger: 'blur'
          }
        ],
        guaranteePeriod: [
          
        ],
        supervisorTel: [
          {
            pattern: /^\d{3}-\d{7,8}|\d{4}-\d{7,8}$/,
            message: '请检查固定电话格式',
            trigger: 'blur'
          }
        ],
        normalTime: [
          { required: true, message: '请输入平均无故障时间', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isEdit() {
      if (this.$route.params && this.$route.params.id) {
        return true
      }
      return false
    },
    id() {
      return this.$route.params && this.$route.params.id
    }
  },
  created() {
    if (this.isEdit) {
      getUserDetail(this.id).then(res => {
        this.form = res.data
      })      
    }
  },
  mounted() {
    
  },
  methods: {
    handleSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            updateUser(this.form).then((res) => {
              if (res) {
                this.$message.success('编辑用户成功')
                this.handleCancel()
              } else {
                this.$message.error('编辑用户失败')
              }
            })
          } else {
            addUser(this.form).then((res) => {
              if (res) {
                this.$message.success('新增用户成功')
                this.handleCancel()
              } else {
                this.$message.error('新增用户失败')
              }
            })
          }
        }
      })
    },
    handleInput(value) {
      this.form.guaranteePeriod = value.replace(/^(0+)|[^\d]+/g, '')
    },
    handleCancel() {
      this.$router.push({
        name: 'UserManage',
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.damConfigForm {
  padding: 40px;
  .title {
    font-size: 20px;
    font-weight: 800;
  }
  .actionBox {
    display: flex;
    justify-content: center;
  }
}
.uploadArea {
  height: 200px;
  background-color: #ccc;
  cursor: pointer;
  border-radius: 4px;
  .iconBox {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .previewImg {
    height: 100%;
    width: 100%;
  }
  .uploadBtn {
    display: none;
  }
  .icon {
    font-size: 80px;
    color: #888;
  }
}
</style>